<?php

namespace app\api\logic\user;

use think\facade\Request;
use app\api\validate\User as UserValidate;
use app\common\repository\user\R as UserRRepository;
use app\common\repository\user\U as UserURepository;

class UserProfile
{
    public function __construct(
        UserRRepository $userRRepository,
        UserURepository $userURepository
    )
    {
        $this->userRRepository = $userRRepository;
        $this->userURepository = $userURepository;
    }

    public function profile()
    {
        return ['data' => $this->userRRepository->getUserProfile(['id' => request()->user->id])];
    }

    public function update()
    {
        $type = Request::param('type');
        $value = Request::param('value');
        $validate = new UserValidate;
        $result = $validate->scene('update')->check(['type' => $type, 'value' => $value]);
        if ($result !== true) {
            return ['done' => false, 'code' => config('code.common.paramsRequire')];
        }
        if ($type == 2) {
            return ['data' => $this->userURepository->updateUserTag(request()->user->id, explode('，', $value))];
        }
        if ($type == 3) {
            return ['data' => $this->userURepository->updateUserSaying(request()->user->id, $value)];
        }
        $update = [];
        if ($type == 0) {
            $update = [
                'avatar' => $value
            ];
        }
        if ($type == 1) {
            $update = [
                'nickname' => $value
            ];
        }
        return ['data' => $this->userURepository->updateUserProfile(request()->user->id, $update)];
    }
}
